package org.itboys.mobile.service.mongo.web;

import org.apache.commons.collections.map.HashedMap;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.entity.mongo.SundayYuyue;
import org.itboys.mobile.entity.mysql.SundayProduct;
import org.itboys.mobile.service.mysql.web.SundayProductService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Auther: 罗尧坤
 * Date: 2018/7/17
 * Description: 集楼_快速预约_web_service
 */

@Service
public class SundayYuyueService extends BaseMongoService<SundayYuyue> {
    @Autowired
    private SundayProductService productService;

    @Autowired
    private SundayMemberService memberService;

    @Resource(name = "mobileDs")
    private MongoDataSource kitchenDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return kitchenDataSource;
    }
    @Override
    protected Class<SundayYuyue> getEntityClass() {
        return SundayYuyue.class;
    }

    /**
     * 分页查询预约记录
     * @param request
     * @return
     */
    public PageResult<SundayYuyue> select(HttpServletRequest request){
        //封装查询条件
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request);
        param.put("isDeleted", 0);
        //查出结果集
        PageResult<SundayYuyue> result = super.pageQuery(request,param);
        //获取预约list
        List<SundayYuyue> data = result.getData();
        if(ListUtil.isNull(data)){
            return result;
        }

        //获取楼盘ids, 用户ids
        List<Long> productIds=new ArrayList<>();
        List<Long> memberIds=new ArrayList<>();
        for (SundayYuyue yuyue:data){
            productIds.add(yuyue.getProductId());
            memberIds.add(yuyue.getMemberId());
        }

        //获取所有用户名和楼盘名
        Map<String,Object> _param=new HashedMap();
        //查出所有对应楼盘
        _param.put("isDeleted", 0);
        _param.put("ids",productIds);
        List<SundayProduct> products = productService.select(_param);
        //清空条件
        _param.clear();
        //查询所有对应用户名
        _param.put("isDeleted", 0);
        _param.put("id in",memberIds);
        List<SundayMember> members = memberService.list(_param);

        //填充productName和memberName到预约记录中
        for (SundayYuyue yuyue:data) {
            for (SundayProduct product:products) {
                if (yuyue.getProductId().equals(product.getId())){
                    yuyue.setProductName(product.getName());
                    break;
                }
            }
            if (yuyue.getProductName()==null){
                yuyue.setProductName("不存在的楼盘");
            }

            for (SundayMember member:members) {
                if (yuyue.getMemberId().equals(member.getId())){
                    yuyue.setMemberName(member.getNickName());
                    break;
                }
            }
            if (yuyue.getMemberName()==null){
                yuyue.setMemberName("找不到对应用户名");
            }
        }
        result.setTotal(result.getTotal());
        result.setData(data);
        return result;
    }

    /**
     * 设置是否已回执用户
     * @param id
     * @param status
     */
    public void setStatus(Long id,Integer status){
        super.update(id,"status",status);
    }

}
